package com.sx.zongzhi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.EventTypeSelectAdapter;
import com.sx.zongzhi.bean.EventTypeBean;
import com.sx.zongzhi.presenter.EventTypePest;
import com.sx.zongzhi.utils.AppConstants;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IEventTypeView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 事件类型选择
 */
public class EventTypeActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener
    ,IEventTypeView{

    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private EventTypeSelectAdapter selectAdapter;
    private List<EventTypeBean> typeBeanList=new ArrayList<>();
    private EventTypePest typePest;


    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.refresh_recyclerview);
        setTitleName(R.string.event_type);
        ButterKnife.bind(this);
        initView();
        onRefresh();
    }

    private void initView() {
        typePest=new EventTypePest(this);
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setColorSchemeResources(AppConstants.colors);
            swipeRefreshLayout.setOnRefreshListener(this);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        selectAdapter = new EventTypeSelectAdapter(this,R.layout.item_event_type,typeBeanList);
        selectAdapter.setEmptyView(View.inflate(this,R.layout.load_error_view,null));
        recyclerView.setAdapter(selectAdapter);
        selectAdapter.getRoadData(new EventTypeSelectAdapter.EventTypeCallback() {
            @Override
            public void getData(EventTypeBean.TypesBean typesBean,String prentName) {
                Intent intent=new Intent();
                intent.putExtra("PrentName",prentName);
                intent.putExtra("EventTypeBeanID",typesBean);
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }


    @Override
    public void onRefresh() {
        typePest.getEventTypeList();
    }


    @Override
    public void showError(String message) {
        if (swipeRefreshLayout.isRefreshing()){
            swipeRefreshLayout.setRefreshing(false);
        }
        ToastUtils.showToast(this,message);
    }

    @Override
    public void getEventTypeList(List<EventTypeBean> eventTypeBeans) {
        swipeRefreshLayout.setRefreshing(false);
        if (eventTypeBeans!=null&&eventTypeBeans.size()>0){
            typeBeanList.clear();
            typeBeanList.addAll(eventTypeBeans);
            selectAdapter.notifyDataSetChanged();
        }
    }
}
